﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MetaView.WPF.Windows
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login
    {
        private readonly TaskCompletionSource<bool> _taskCompletionSource = new();

        public Login()
        {
            InitializeComponent();
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            for (var i = 5; i > 0; i--)
            {
                _continueButton.Content = $"Continue ({i})";
                await Task.Delay(TimeSpan.FromSeconds(1));
            }

            _continueButton.Content = "Continue";
            _continueButton.IsEnabled = true;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            _taskCompletionSource.TrySetResult(false);
        }

        private void Continue_Click(object sender, RoutedEventArgs e)
        {
            _taskCompletionSource.TrySetResult(true);
            Close();

            RestartMainWindow();
        }

        public void RestartMainWindow()
        {
            var mainWindow = new MainWindow
            {
                WindowStartupLocation = WindowStartupLocation.CenterScreen
            };
            mainWindow.Show();
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            _taskCompletionSource.TrySetResult(false);
            Close();
        }
    }
}
